package com.smartwork.common.mobile.wechat.util;

import com.alibaba.fastjson.JSONObject;
import com.smartwork.common.mobile.wechat.constant.StaticValue;
import com.smartwork.common.mobile.wechat.thread.TokenThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TemplateMessageUtil {
	
	private static Logger log = LoggerFactory.getLogger(WeixinUtil.class);

	/**
	 * 发送模板消息
	 * @param jsonString
	 * @return true:success false: fail
	 */
	public static boolean sendMessage (String groupId, String jsonString) {
		String url = String.format(StaticValue.massTmplMsgUrl, TokenThread.getAccessTokenStr(groupId));
	    JSONObject jsonObject = WeixinUtil.httpRequest(url, "POST", jsonString);
        int result = 0;
        if (null != jsonObject) {  
             if (0 != jsonObject.getIntValue("errcode")) {  
                 result = jsonObject.getIntValue("errcode");  
                 log.error("错误 errcode:{} errmsg:{}", jsonObject.getIntValue("errcode"), jsonObject.getString("errmsg"));  
                 return false;
             } 
        }
        log.debug("模板消息发送结果：{}", result);
        return true;
	}
}
